package com.itheima.controller;

import com.itheima.exception.MyFileException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @description:
 * @author: 付益民
 * @create: 2020-09-29-16:27
 */
@RestController
@RequestMapping("/stu")
public class StudentController {
   /* @RequestMapping("/{id}")
    public Object student(@PathVariable("id") Integer id) {
        Map<String, Object> stu = new HashMap<>();
        stu.put("name", "祖宗");
        stu.put("age", "1000");
        stu.put("id", id);
        return stu;
    }*/

    @PostMapping("/upload")
    public Map upload( String username, String password, MultipartFile[] fileName, HttpServletRequest request) throws MyFileException {
        System.out.println(username);
        System.out.println(password);

        String realPath = request.getServletContext().getRealPath("/WEB-INF/uploads");
        Map<String, Object> mp = new HashMap<>();
        try {
            for (MultipartFile multipartFile : fileName) {
                if (!multipartFile.isEmpty()) {
                    System.out.println(multipartFile.getOriginalFilename());
                    multipartFile.transferTo(new File(realPath,UUID.randomUUID().toString()+ multipartFile.getOriginalFilename()));

                }
            }
            mp.put("flag", true);
            mp.put("message", "上传成功");
            return mp;
        } catch (IOException e) {
            throw new MyFileException("文件上传失败");
        }

    }






    //接收GET请求简化配置方式
    @GetMapping("/{id}")
    public String get(@PathVariable Integer id){
        System.out.println("restful is running ....get:"+id);
        return "success.jsp";
    }


    //接收POST请求简化配置方式
    @PostMapping("/{id}")
    public String post(@PathVariable Integer id){
        System.out.println("restful is running ....post:"+id);
        return "success.jsp";
    }


    //接收PUT请求简化配置方式
    @PutMapping("/{id}")
    public String put(@PathVariable Integer id){
        System.out.println("restful is running ....put:"+id);
        return "success.jsp";
    }

    @DeleteMapping("/{id}")
    public String delete(@PathVariable Integer id){
        System.out.println("restful is running ....put:"+id);
        return "success.jsp";
    }
}
